<template>
    <div>
        <a-modal
                width="360px"
                :closable="false"
                style="z-index: 9999"
                title="授权新管理员"
                :visible="show"
                @ok="add"
                @cancel="show=false"
                cancelText="关闭"
                okText="提交">
            <span style="font-size: small">
                输入被授权账号的邮箱
            </span>
            <a-input placeholder="邮箱..." v-model="email"/>
            <span style="font-size: small">
                1.仅拥有学生权限的账号可以被授权。
                <br/>
                2.后台只能添加管理员，超级管理员请联系开发者进行授权。
            </span>
        </a-modal>

        <strong style="font-size: large">共{{list.length}}位</strong>
        <span v-if="user.type==-1">
            <a-divider type="vertical"/>
            <a-button type="primary" @click="show = true">授权新管理员</a-button>
        </span>
        <a-list style="margin-top: 10px">
            <a-list-item v-for="i in list" :key="i.id">
                <a-list-item-meta>
                    <a slot="title" style="font-weight: bold" target="_blank" :href="'https://jxjlife.cn/user/'+i.id">{{i.name}}</a>
                </a-list-item-meta>
                {{i.email}}
                <span style="float: right">
                    <a-tag color="orange" v-if="i.type==0">
                        管理员
                    </a-tag>
                    <a-tag color="orange" v-if="i.type==-1">
                        超级管理员
                    </a-tag>
                    <a-button type="danger" size="small" @click="revoke(i.id)" v-if="user.type==-1">撤权</a-button>
                </span>
            </a-list-item>
        </a-list>
    </div>
</template>

<script>
    export default {
        name: "Admins",
        methods:{
            revoke(id){
                const t = this;
                axios.get('/admin/revoke?id='+id).then(function (resp) {
                    t.getData();
                    t.$message.success('已执行');

                })
            },
            getData(){
                const t = this;
                axios.get('/admin/getAllAdmins').then(function (resp) {
                    t.list = resp.data;
                })
            },
            add(){
                const t = this;
                if (t.email.match(/^\s*$/)){
                    return;
                }
                axios.get('/admin/add?type=0&em='+t.email).then(function (resp) {
                    t.getData();
                    if (resp.data){
                        t.$message.success('已执行');
                        t.email = '';
                        t.show = false;
                    } else{
                        alert('该账号不可被授权');
                    }
                })
            }
        },
        data(){
            return{
                show: false,
                email: '',
                list: [],
                user: '',
            }
        },
        created() {
            const t = this;
            axios.get('/user/getUser').then(function (resp) {
                t.user = resp.data;
            })
            this.getData();
        }
    }
</script>

<style scoped>

</style>